package com.example.itcacp.repository.mybatis;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.itcacp.entity.Authority;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface AuthorityMapper extends BaseMapper<Authority> {

    /**
     * 查询所有权限
     */
    @SelectProvider(type = AuthSqlProvider.class, method = "selectAuthAll")
    @Results({
            @Result(property = "authorityId", column = "authority_id"),
            @Result(property = "authorityName", column = "authority_name"),
            @Result(property = "description", column = "description"),
            @Result(property = "path", column = "path")
    })
    List<Authority> selectAuthAll(String authorityName);
    // 动态Sql
    class AuthSqlProvider {
        public String selectAuthAll(String authorityName){
            return new SQL () {{
                SELECT ( "*" );
                FROM ( "it_authority" );
                boolean hasWhere = false;  // 记录是否已经存在where子句
                if (authorityName != null && !authorityName.isEmpty ()) {
                    WHERE ( "authority_name LIKE CONCAT('%', #{authorityName}, '%')" ); // like模糊查询
                    hasWhere = true;  // 存在则需要添加and子句
                }
                ORDER_BY ( "authority_id DESC" );  //倒序排序
            }}.toString ();
        }
    }
}
